import Vue from 'vue'
export default {
	// 收到私聊消息
	recordConversation(state,data){
		const response=data.data||data
		const {send,receive,time}=response
		if(send && receive){
			let isSelf = send==state.userInfo.id
			let id=isSelf?receive:send;
			if(!state.chatRecord[id]){
				Vue.set(state.chatRecord,id,[])
			}
			response.isSelf=isSelf
			state.chatRecord[id].push(response)
			// 设置末读
			const index=state.chatReminder.findIndex(item=>{
				return item.receive === id
			})
			if(index > -1){
				const obj = state.chatReminder.splice(index,1)
				const  number= obj[0].number+1
				state.chatReminder.unshift({receive:id,time,number})
			}else{
				state.chatReminder.unshift({
					time,
					number:1,
					receive:id
				})
			}
			
		}
	},
	// 拉取私聊信息
	recordList(state,data){
		const response=data.data||data
		const {send,receive,list} =response,id=state.userInfo.id
		if(send && receive && list){
			const arr =list.map(item=>{
				const mes= JSON.parse(item)
				mes.isSelf= mes.send === id
				return mes
			})
			Vue.set(state.chatRecord,receive,arr)
		}			
	},
	// 从服务端拉取用户列表后 返回用户列表 data={id:{id,nickname,...},id:{id,nickname,...}}
	recordUserList(state,data){
		
		data=data.data||data
		
		if(!data || data.length===0){
			return;
		}
		//console.log(data)
		const arr=Object.keys(data)
		arr.forEach(key=>{
			Vue.set(state.userInfoObject,key,JSON.parse(data[key]))
		})
	},
	//末读信息列表
	recordLastReading(state,data){
		data=data.data||data
		const {read,number}=data
		if(read){
			const arr=Object.keys(read)
			state.chatReminder = arr.map(item=>{
				const obj={receive:item*1,time:read[item]}
				if(number[item]){
					obj.number=number[item]*1
				}
				return obj
			}).sort((a,b)=>{
				return b.time-a.time
			})
		}
	},
	
}